﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class GameSetPage : EventManager {

   // public Toggle forbidPlyaerVoiceToggle;
    private AudioSource bgMusicAud;
    private AudioSource aud1;
    private AudioSource aud2;
    public List<Slider> sliders;
    public Text versionNum;
    public Button changeAccountBtn;
    public Button userAgreeMentBtn;
    public Button closeBtn;

    public override void InformationSetting()
    {
        sliders[0].value  = PlayerPrefs.GetFloat("MusicSet", 0.3f);
        sliders[1].value  = PlayerPrefs.GetFloat("AudioSet", 0.3f);
        SetText(versionNum, string.Format("版本号{0}", DefaultKeyConst.version));
        if (GameObject.Find("GameRoomPage(Clone)"))
        {
            changeAccountBtn.gameObject.SetActive(false);
            userAgreeMentBtn.gameObject.SetActive(false);
        }
    }

    new void Start()
    {
        bgMusicAud = GameObject.Find("StaticSource").GetComponent<AudioSource>();
        GameObject cav = GameObject.Find("Canvas").gameObject;
        aud1 = cav.transform.GetComponents<AudioSource>()[0];
        aud2 = cav.transform.GetComponents<AudioSource>()[1];

        sliders[0].onValueChanged.AddListener(BgMusicValueChanged);
        sliders[1].onValueChanged.AddListener(AudValueChanged);
        AddListenerToButton(userAgreeMentBtn, UserAgreeMentBtnClick);
        AddListenerToButton(changeAccountBtn, ChangeAccountBtnClick);
        AddListenerToButton(closeBtn, CloseBtnClick);

        //forbidPlyaerVoiceToggle.onValueChanged.AddListener(ForbidPlyaerVoiceClick);
    }

    void BgMusicValueChanged(float value)
    {
        bgMusicAud.volume = value;
        PlayerPrefs.SetFloat("MusicSet", bgMusicAud.volume);
        PlayerPrefs.Save();
    }

    void AudValueChanged(float value)
    {
        aud1.volume = value;
        aud2.volume = value;
        PlayerPrefs.SetFloat("AudioSet", aud1.volume);
        PlayerPrefs.Save();
    }


    void UserAgreeMentBtnClick()
    {
        UIManager.ChangeUI(UIManager.PageState.UserAgreeMentPage, (GameObject obj) =>
        {
            obj.GetComponent<UserAgreeMentPage>().InformationSetting();
        });
    }

    void ChangeAccountBtnClick()
    {
        PlayerPrefs.DeleteAll();
        Application.Quit();
    }

    //战斗场景中生成
    public void FightScenceSet()
    {
        InformationSetting();
        userAgreeMentBtn.gameObject.SetActive(false);
        changeAccountBtn.gameObject.SetActive(false);
    }

    #region
    // void  ForbidPlyaerVoiceClick(bool isOn)
    //{
    //    if (isOn)
    //    {
    //        GameData.GetInstance().forbidPlayerVoice = true;
    //    }
    //    else
    //    {
    //        GameData.GetInstance().forbidPlayerVoice = false;
    //    }
    //}
    #endregion
}
